package cn.wolfcode.wolf2world.web.controller;

import cn.wolfcode.wolf2world.domain.Strategy;
import cn.wolfcode.wolf2world.domain.StrategyCatalog;
import cn.wolfcode.wolf2world.domain.StrategyTheme;
import cn.wolfcode.wolf2world.query.StrategyQueryObject;
import cn.wolfcode.wolf2world.service.StrategyCatalogService;
import cn.wolfcode.wolf2world.service.StrategyService;
import cn.wolfcode.wolf2world.service.StrategyThemeService;
import cn.wolfcode.wolf2world.vo.CatalogVO;
import cn.wolfcode.wolf2world.vo.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 杨兴健
 * @Date 2020-08-11 19:00
 */
@Controller
@RequestMapping("/strategy")
public class StrategyController {
    @Autowired
    private StrategyService strategyService;

    @Autowired
    private StrategyCatalogService strategyCatalogService;
    @Autowired
    private StrategyThemeService strategyThemeService;

    /**
     * 目的地高级查询
     *
     * @param model
     * @param qo    查询条件
     * @return
     */
    @RequestMapping("/list")
    public String query(Model model, @ModelAttribute("qo") StrategyQueryObject qo) {
        Page<Strategy> page = strategyService.query(qo);
        model.addAttribute("page", page);
        return "strategy/list";
    }

    @RequestMapping("/input")
    public String input(String id, Model model) {
        if (StringUtils.hasLength(id)) {
            Strategy strategy = strategyService.get(id);
            model.addAttribute("strategy", strategy);
        }

        List<CatalogVO> list = strategyCatalogService.queryCatalogVO();
        model.addAttribute("catalogs", list);

        List<StrategyTheme> themes = strategyThemeService.list();
        model.addAttribute("themes", themes);

        return "strategy/input";
    }

    /**
     * 新增或更新
     *
     * @param strategy
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy) {
        strategyService.saveOrUpdate(strategy);
        return JsonResult.success();
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        strategyService.delete(id);
        return JsonResult.success();
    }
}
